package io.atom.modules.security.service.impl;

import io.atom.common.constant.Constant;
import io.atom.common.constant.JwtClaimsConstant;
import io.atom.common.properties.JwtProperties;
import io.atom.common.service.impl.BaseServiceImpl;
import io.atom.common.utils.JwtUtil;
import io.atom.modules.security.oauth2.TokenGenerator;
import io.atom.common.utils.Result;
import io.atom.modules.security.dao.SysUserTokenDao;
import io.atom.modules.security.entity.SysUserTokenEntity;
import io.atom.modules.security.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {

	@Autowired
	private JwtProperties jwtProperties;

	@Override
	public Result createToken(Long userId) {
		//登录成功后，生成jwt令牌
		Map<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.USER_ID, userId);
		String token = JwtUtil.createJWT(
				jwtProperties.getAdminSecretKey(),
				jwtProperties.getAdminTtl(),
				claims);

		Map<String, Object> map = new HashMap<>(2);
		map.put(Constant.TOKEN_HEADER, token);
		return new Result().ok(map);
	}

	@Override
	public void logout(Long userId) {
		//生成一个token
		String token = TokenGenerator.generateValue();

		//修改token
		baseDao.updateToken(userId, token);
	}
}